﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Grid;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using Dictionary;
    using HRM.CORE;
    
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms; using Common;
    using DevExpress.XtraGrid.Views.Base;

    public class xucRelative : UserControl
    {
        private BarDockControl barDockControlBottom;
        private BarDockControl barDockControlLeft;
        private BarDockControl barDockControlRight;
        private BarDockControl barDockControlTop;
        private BarManager barManager1;
        private BarButtonItem bbiDelete;
        private GridColumn colAddress;
        private GridColumn colBirthday;
        private GridColumn colEmail;
        private GridColumn colEmployeeCode;
        private GridColumn colIncomeTaxCode;
        private GridColumn colIsDepend;
        private GridColumn colPersonName;
        private GridColumn colPhone;
        private GridColumn colRelative;
        private IContainer components;
        private GridView gbList;
        private GridControl gcList;
        private GridColumn gridColumn1;
        private GridColumn gridColumn2;
        private string m_EmployeeCode;
        private PopupMenu ppMenu;
        private GridView repositoryItemGridLookUpEdit1View;
        private RepositoryItemGridLookUpEdit repRelative;
        private RepositoryItemPictureEdit rptPhoto;

        public xucRelative()
        {
            this.m_EmployeeCode = "";
            this.components = null;
            this.InitializeComponent();
            this.InitMultiLanguages();
        }

        public xucRelative(string EmployeeCode)
        {
            this.m_EmployeeCode = "";
            this.components = null;
            this.InitializeComponent();
            this.InitMultiLanguages();
            this.m_EmployeeCode = EmployeeCode;
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.gbList.DeleteSelectedRows();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.gbList.DeleteSelectedRows();
            }
            if (e.KeyCode == Keys.F5)
            {
                this.Init();
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            }
        }

        public void Init()
        {
            this.barManager1.SetPopupContextMenu(this.gcList, this.ppMenu);
            new DIC_RELATIVE().AddRepositoryGridLookup(this.repRelative);
            this.repRelative.ButtonClick += new ButtonPressedEventHandler(this.repRelative_ButtonClick);
            this.gcList.DataSource = new HRM_EMPLOYEE_RELATIVE().GetList(this.m_EmployeeCode);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(HRM.CORE.xucRelative));
            SerializableAppearanceObject appearance = new SerializableAppearanceObject();
            this.gcList = new GridControl();
            this.gbList = new GridView();
            this.colPersonName = new GridColumn();
            this.colEmployeeCode = new GridColumn();
            this.colRelative = new GridColumn();
            this.repRelative = new RepositoryItemGridLookUpEdit();
            this.repositoryItemGridLookUpEdit1View = new GridView();
            this.gridColumn2 = new GridColumn();
            this.colBirthday = new GridColumn();
            this.colAddress = new GridColumn();
            this.colEmail = new GridColumn();
            this.colPhone = new GridColumn();
            this.colIncomeTaxCode = new GridColumn();
            this.colIsDepend = new GridColumn();
            this.rptPhoto = new RepositoryItemPictureEdit();
            this.gridColumn1 = new GridColumn();
            this.barManager1 = new BarManager(this.components);
            this.barDockControlTop = new BarDockControl();
            this.barDockControlBottom = new BarDockControl();
            this.barDockControlLeft = new BarDockControl();
            this.barDockControlRight = new BarDockControl();
            this.bbiDelete = new BarButtonItem();
            this.ppMenu = new PopupMenu(this.components);
            this.gcList.BeginInit();
            this.gbList.BeginInit();
            this.repRelative.BeginInit();
            this.repositoryItemGridLookUpEdit1View.BeginInit();
            this.rptPhoto.BeginInit();
            this.barManager1.BeginInit();
            this.ppMenu.BeginInit();
            base.SuspendLayout();
            this.gcList.Dock = DockStyle.Fill;
            this.gcList.EmbeddedNavigator.Buttons.Append.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.CancelEdit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.Edit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.EndEdit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.Remove.Visible = false;
            this.gcList.EmbeddedNavigator.TextStringFormat = "{0}/{1}";
            this.gcList.Location = new Point(0, 0);
            this.gcList.MainView = this.gbList;
            this.gcList.Name = "gcList";
            this.gcList.RepositoryItems.AddRange(new RepositoryItem[] { this.rptPhoto, this.repRelative });
            this.gcList.Size = new Size(0x30b, 0x1de);
            this.gcList.TabIndex = 5;
            this.gcList.UseEmbeddedNavigator = true;
            this.gcList.ViewCollection.AddRange(new BaseView[] { this.gbList });
            this.gbList.Appearance.GroupRow.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.gbList.Appearance.GroupRow.Options.UseFont = true;
            this.gbList.Appearance.HeaderPanel.Options.UseTextOptions = true;
            this.gbList.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;
            this.gbList.Appearance.HeaderPanel.TextOptions.WordWrap = WordWrap.Wrap;
            this.gbList.Appearance.Row.Options.UseTextOptions = true;
            this.gbList.Appearance.Row.TextOptions.WordWrap = WordWrap.Wrap;
            this.gbList.BorderStyle = BorderStyles.NoBorder;
            this.gbList.ColumnPanelRowHeight = 0x24;
            this.gbList.Columns.AddRange(new GridColumn[] { this.colPersonName, this.colEmployeeCode, this.colRelative, this.colBirthday, this.colAddress, this.colEmail, this.colPhone, this.colIncomeTaxCode, this.colIsDepend });
            this.gbList.GridControl = this.gcList;
            this.gbList.GroupPanelText = "K\x00e9o v\x00e0 Thả cột v\x00e0o đ\x00e2y để nh\x00f3m dữ liệu";
            this.gbList.IndicatorWidth = 40;
            this.gbList.Name = "gbList";
            this.gbList.OptionsBehavior.AllowIncrementalSearch = true;
            this.gbList.OptionsBehavior.AutoExpandAllGroups = true;
            this.gbList.OptionsBehavior.CopyToClipboardWithColumnHeaders = false;
            this.gbList.OptionsBehavior.FocusLeaveOnTab = true;
            this.gbList.OptionsLayout.StoreAllOptions = true;
            this.gbList.OptionsLayout.StoreAppearance = true;
            this.gbList.OptionsMenu.EnableColumnMenu = false;
            this.gbList.OptionsMenu.EnableGroupPanelMenu = false;
            this.gbList.OptionsNavigation.EnterMoveNextColumn = true;
            this.gbList.OptionsSelection.InvertSelection = true;
            this.gbList.OptionsSelection.MultiSelect = true;
            this.gbList.OptionsView.ColumnAutoWidth = false;
            this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            this.gbList.OptionsView.RowAutoHeight = true;
            this.gbList.OptionsView.ShowAutoFilterRow = true;
            this.gbList.OptionsView.ShowGroupPanel = false;
            this.gbList.KeyDown += new KeyEventHandler(this.gbList_KeyDown);
            this.colPersonName.Caption = "Họ t\x00ean";
            this.colPersonName.FieldName = "PersonName";
            this.colPersonName.Name = "colPersonName";
            this.colPersonName.Visible = true;
            this.colPersonName.VisibleIndex = 0;
            this.colPersonName.Width = 0x6d;
            this.colEmployeeCode.Caption = "M\x00e3 nh\x00e2n vi\x00ean";
            this.colEmployeeCode.FieldName = "EmployeeCode";
            this.colEmployeeCode.Name = "colEmployeeCode";
            this.colRelative.Caption = "Mối quan hệ";
            this.colRelative.ColumnEdit = this.repRelative;
            this.colRelative.FieldName = "Relative";
            this.colRelative.Name = "colRelative";
            this.colRelative.Visible = true;
            this.colRelative.VisibleIndex = 1;
            this.colRelative.Width = 0x43;
            this.repRelative.AutoHeight = false;
            this.repRelative.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo), new EditorButton(ButtonPredefines.Glyph, "Add", -1, true, true, false, ImageLocation.MiddleCenter, (Image) manager.GetObject("repRelative.Buttons"), new KeyShortcut(Keys.None), appearance, "", null, null, true) });
            this.repRelative.Name = "repRelative";
            this.repRelative.NullText = "";
            this.repRelative.View = this.repositoryItemGridLookUpEdit1View;
            this.repositoryItemGridLookUpEdit1View.Columns.AddRange(new GridColumn[] { this.gridColumn2 });
            this.repositoryItemGridLookUpEdit1View.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            this.repositoryItemGridLookUpEdit1View.Name = "repositoryItemGridLookUpEdit1View";
            this.repositoryItemGridLookUpEdit1View.OptionsSelection.EnableAppearanceFocusedCell = false;
            this.repositoryItemGridLookUpEdit1View.OptionsView.ShowGroupPanel = false;
            this.gridColumn2.Caption = "Mối quan hệ";
            this.gridColumn2.FieldName = "RelativeName";
            this.gridColumn2.Name = "gridColumn2";
            this.gridColumn2.Visible = true;
            this.gridColumn2.VisibleIndex = 0;
            this.colBirthday.Caption = "Ng\x00e0y sinh";
            this.colBirthday.FieldName = "Birthday";
            this.colBirthday.Name = "colBirthday";
            this.colBirthday.Visible = true;
            this.colBirthday.VisibleIndex = 2;
            this.colAddress.Caption = "Địa chỉ";
            this.colAddress.FieldName = "Address";
            this.colAddress.Name = "colAddress";
            this.colAddress.Visible = true;
            this.colAddress.VisibleIndex = 3;
            this.colAddress.Width = 220;
            this.colEmail.Caption = "Email";
            this.colEmail.FieldName = "Email";
            this.colEmail.Name = "colEmail";
            this.colEmail.Visible = true;
            this.colEmail.VisibleIndex = 4;
            this.colEmail.Width = 0x98;
            this.colPhone.Caption = "Số điện thoại";
            this.colPhone.FieldName = "Phone";
            this.colPhone.Name = "colPhone";
            this.colPhone.Visible = true;
            this.colPhone.VisibleIndex = 5;
            this.colPhone.Width = 0x54;
            this.colIncomeTaxCode.Caption = "M\x00e3 số thuế";
            this.colIncomeTaxCode.FieldName = "IncomeTaxCode";
            this.colIncomeTaxCode.Name = "colIncomeTaxCode";
            this.colIncomeTaxCode.Visible = true;
            this.colIncomeTaxCode.VisibleIndex = 6;
            this.colIncomeTaxCode.Width = 0x66;
            this.colIsDepend.Caption = "Đối tượng phụ thuộc";
            this.colIsDepend.FieldName = "IsDepend";
            this.colIsDepend.Fixed = FixedStyle.Right;
            this.colIsDepend.Name = "colIsDepend";
            this.colIsDepend.Visible = true;
            this.colIsDepend.VisibleIndex = 7;
            this.colIsDepend.Width = 0x47;
            this.rptPhoto.CustomHeight = 0x30;
            this.rptPhoto.Name = "rptPhoto";
            this.rptPhoto.NullText = "[Chưa chọn h\x00ecnh]";
            this.rptPhoto.SizeMode = PictureSizeMode.Zoom;
            this.gridColumn1.Caption = "Mối quan hệ";
            this.gridColumn1.FieldName = "RelativeName";
            this.gridColumn1.Name = "gridColumn1";
            this.gridColumn1.Visible = true;
            this.gridColumn1.VisibleIndex = 0;
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Items.AddRange(new BarItem[] { this.bbiDelete });
            this.barManager1.MaxItemId = 1;
            this.bbiDelete.Caption = "X\x00f3a";
            this.bbiDelete.Id = 0;
            this.bbiDelete.Name = "bbiDelete";
            this.bbiDelete.ItemClick += new ItemClickEventHandler(this.bbiDelete_ItemClick);
            this.ppMenu.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiDelete) });
            this.ppMenu.Manager = this.barManager1;
            this.ppMenu.Name = "ppMenu";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.gcList);
            base.Controls.Add(this.barDockControlLeft);
            base.Controls.Add(this.barDockControlRight);
            base.Controls.Add(this.barDockControlBottom);
            base.Controls.Add(this.barDockControlTop);
            base.Name = "xucRelative";
            base.Size = new Size(0x30b, 0x1de);
            this.gcList.EndInit();
            this.gbList.EndInit();
            this.repRelative.EndInit();
            this.repositoryItemGridLookUpEdit1View.EndInit();
            this.rptPhoto.EndInit();
            this.barManager1.EndInit();
            this.ppMenu.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            this.colPersonName.Caption = MultiLanguages.GetString("tbl_EmployeeRelative", "PersonName", this.colPersonName.Caption);
            this.colRelative.Caption = MultiLanguages.GetString("tbl_EmployeeRelative", "Relative", this.colRelative.Caption);
            this.colAddress.Caption = MultiLanguages.GetString("tbl_EmployeeRelative", "Address", this.colAddress.Caption);
            this.colEmail.Caption = MultiLanguages.GetString("tbl_EmployeeRelative", "Email", this.colEmail.Caption);
            this.colPhone.Caption = MultiLanguages.GetString("tbl_EmployeeRelative", "Phone", this.colPhone.Caption);
            this.colIsDepend.Caption = MultiLanguages.GetString("tbl_EmployeeRelative", "IsDepend", this.colIsDepend.Caption);
        }

        private void repRelative_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            xfmRelativeAdd.AddedEventHander hander = null;
            if (e.Button.Caption == "Add")
            {
                xfmRelativeAdd add = new xfmRelativeAdd(Actions.Add);
                if (hander == null)
                {
                    hander = (s, i) => new DIC_RELATIVE().AddRepositoryGridLookup(this.repRelative);
                }
                add.Added += hander;
                add.ShowDialog();
            }
        }

        public void Save()
        {
            HRM_EMPLOYEE_RELATIVE hrm_employee_relative = new HRM_EMPLOYEE_RELATIVE();
            hrm_employee_relative.Delete(this.m_EmployeeCode);
            try
            {
                foreach (DataRow row in (this.gcList.DataSource as DataTable).Rows)
                {
                    try
                    {
                        if (row != null)
                        {
                            hrm_employee_relative.Insert(Guid.NewGuid(), this.m_EmployeeCode, row["PersonName"].ToString(), row["Relative"].ToString(), (row["Birthday"].ToString() != "") ? Convert.ToDateTime(row["Birthday"].ToString()) : System.DateTime.Now, row["Address"].ToString(), row["Email"].ToString(), row["Phone"].ToString(), row["IncomeTaxCode"].ToString(), bool.Parse(row["IsDepend"].ToString()));
                        }
                    }
                    catch
                    {
                        if (row != null)
                        {
                            hrm_employee_relative.Insert(Guid.NewGuid(), this.m_EmployeeCode, row["PersonName"].ToString(), row["Relative"].ToString(), (row["Birthday"].ToString() != "") ? Convert.ToDateTime(row["Birthday"].ToString()) : System.DateTime.Now, row["Address"].ToString(), row["Email"].ToString(), row["IncomeTaxCode"].ToString(), row["Phone"].ToString(), false);
                        }
                    }
                }
            }
            catch
            {
            }
        }
    }
}

